package com.store.web.security;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.dao.CategoryDAO;
import com.ross.web.dao.ProductDAO;
import com.ross.web.domain.Category;
import com.ross.web.domain.Product;
import com.ross.web.util.SessionLogout;

public class AuditStocksAction extends ActionSupport {
	
	private List<Product> productList;
	private List<Category> categoryList;
	private Category category;
	private int dispMode;
	private int c_id;
	private String prodName;
	private String prodBarCode;
	private Date startExpiryDate;
	private Date endExpiryDate;
	private Product product;
	private ProductDAO productDAO = new ProductDAO();
	
	CategoryDAO categoryDAO = new CategoryDAO();
	
	HttpServletRequest request = ServletActionContext.getRequest();
	private SessionLogout sessionLogout = new SessionLogout();
	
	
	
	public String goToAuditStocksAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
			

		if(((String)request.getParameter("out")).equalsIgnoreCase("yes")){
		setDispMode(3);
		productList = productDAO.getAllProductOutofStock();
		
		}else{
		setDispMode(1);
		categoryList = categoryDAO.getAllCategory();
		productList = productDAO.getAllProduct();
		}
		return SUCCESS;
	}
	
	public String searchStockAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		if(!validateInputs()){
			categoryList = categoryDAO.getAllCategory();
			setDispMode(1);
			return "inputErr";
			
		}else{
			
			categoryList = categoryDAO.getAllCategory();
			System.out.println("eto category");
			productList = productDAO.getProductByKeywords(prodName,prodBarCode,startExpiryDate,endExpiryDate,c_id);
			setDispMode(1);
			
			addActionMessage("Query returned "+productList.size()+" results");
			
			return SUCCESS;
		}
		
		
		
	}
	


	public String goToViewStockAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		categoryList = categoryDAO.getAllCategory();
		product = productDAO.getProductById(product.getProdId());
		setC_id(product.getCategory().getC_id());
		setDispMode(2);
		return SUCCESS;
	}
	
	
	
	private boolean validateInputs(){
		
		
		if(prodName.trim().length() == 0){
			addActionError(getText("error.auditstocks.prodnamerequired"));
		}
		
		if(startExpiryDate!=null && endExpiryDate!=null){
			if(endExpiryDate.compareTo(startExpiryDate)<1){
				addActionError(getText("error.auditstocks.dateerror"));
			}
		}else if(startExpiryDate==null&&endExpiryDate!=null || startExpiryDate!=null&&endExpiryDate==null){
			addActionError(getText("error.auditstocks.dateerror"));
		}
		
		if(this.hasActionErrors()){
			return false;
		}else{
			return true;
		}
	}
	
	public List<Product> getProductList() {
		return productList;
	}


	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}


	public List<Category> getCategoryList() {
		return categoryList;
	}


	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}


	public Category getCategory() {
		return category;
	}


	public void setCategory(Category category) {
		this.category = category;
	}


	public int getDispMode() {
		return dispMode;
	}


	public void setDispMode(int dispMode) {
		this.dispMode = dispMode;
	}


	public SessionLogout getSessionLogout() {
		return sessionLogout;
	}


	public void setSessionLogout(SessionLogout sessionLogout) {
		this.sessionLogout = sessionLogout;
	}

	public int getC_id() {
		return c_id;
	}

	public void setC_id(int c_id) {
		this.c_id = c_id;
	}

	public String getProdName() {
		return prodName;
	}

	public void setProdName(String prodName) {
		this.prodName = prodName;
	}

	public String getProdBarCode() {
		return prodBarCode;
	}

	public void setProdBarCode(String prodBarCode) {
		this.prodBarCode = prodBarCode;
	}

	public Date getStartExpiryDate() {
		return startExpiryDate;
	}

	public void setStartExpiryDate(Date startExpiryDate) {
		this.startExpiryDate = startExpiryDate;
	}

	public Date getEndExpiryDate() {
		return endExpiryDate;
	}

	public void setEndExpiryDate(Date endExpiryDate) {
		this.endExpiryDate = endExpiryDate;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}



}
